// 数组的创建
let arr1 = [1, 2, 3, 5, 56]
let arr2: [Int] = []
let arr3 = [Int]()
let arr4 = Array<String>()
let arr5 = Array(repeating: "Z", count: 5)
let arr6 = [Int](0...7)
let person = ["zhangsan": 27, "lisi": 28, "wangwu": 20]
let arr7 = [String](person.keys) // ["zhangsan", "lisi", "wangwu"]

// 数组的遍历
print("------------ for in ------------")
for i in arr1 {
    print(i)
}

print("------------ forEach ------------")
arr7.forEach { (name) in
    print("name is \(name)")
}

print("------------ enumrated ------------")
for (index, num) in arr6.enumerated() {
    print("index is \(index), num is \(num)")
}

print("------------ iterator ------------")
var iter = arr7.makeIterator()
while let name = iter.next() {
    print("name is \(name)")
}

print("------------ indices ------------")
for i in arr7.indices {
    print("name is \(arr7[i])")
}

// 数组的查找
print("------------ contains ------------")
print(arr7.contains("zhangsan"))
print(arr7.contains("zhangsang"))
print(
    arr7.contains { String in
        String.count > 8
    }
)
print(
    arr7.contains { String in
        String.count > 5
    }
)

print("------------ allSatisfy(全部符合) ------------")
print(
    arr6.allSatisfy { Int in
        Int > -1
    }
)
print(
    arr6.allSatisfy { Int in
        Int > 3
    }
)

print("------------ first, last ------------")
print(arr6.first ?? 123456)
print(arr6.last ?? 123456)
print(arr6.first(where: {
    // 返回从左到右第一个满足条件的元素, 如果都不满足就返回nil
    $0 > 0
}) ?? 111111)

print("------------ firstIndex, lastIndex ------------")
print(arr6.firstIndex(of: 3) ?? 10)

print("------------ max(:by), min(:by) ------------")
var array = [(45, "error1"), (23, "error2"), (30, "error3")]
print(array.min {a, b in a.0 < b.0} ?? 123456)
print(array.min(by: { (a, b) -> Bool in
    a.0 < b.0
}) ?? 123456)

print("------------ append(末尾添加元素) ------------")
var arr8 = [Int](2...7)
arr8.append(50922)
print(arr8)
arr8.append(contentsOf: 50922...50929)
print(arr8)

print("------------ insert(任意位置添加元素) ------------")
var arr9 = [Int](2...7)
arr9.insert(100, at: 0)
print(arr9)
arr9.insert(contentsOf: 100...110, at: 2)
print(arr9)

var arr10: [Character] = ["a", "b", "c"]
arr10.insert(contentsOf: "Greeting", at: 2)
print(arr10)

print("------------ remove(:at) ------------")
var arr11 = [Int](2...7)
print(arr11.removeFirst())
print(arr11)
arr11.remove(at: 2)
print(arr11)
print(arr11.popLast() as Any)
print(arr11)
print(arr11.removeLast())
print(arr11)

print("------------ removeSubrange ------------")
var arr12 = ["h", "e", "l", "l", "o", "w"]
arr12.removeSubrange(1...3)
print(arr12)
// arr12.removeAll(keepingCapacity: true)
// print(arr12.capacity)
